home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1997 August / Walnut Creek CDROM.7z / LISTINGS / V_13_11 / WEISFELD.ZIP / TSEMA.CPP < prev   
Encoding:
C/C++ Source or Header  |  1995-09-11  |  1.0 KB  |  51 lines

  1. //**************************************************
  2. //  Listing 4
  3. //
  4. //  FILE NAME   : tsema.cpp
  5. //  AUTHOR      : Matt Weisfeld
  6. //
  7. //  DESCRIPTION : test the semaphore class
  8. //**************************************************
  9. #include <iostream.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "sema.h"
  13.  
  14. const PROC_NUM=3;   // this can be passed as a parameter
  15.  
  16. //
  17. // Simple test program for using the semaphore class. To initialize
  18. // the synchronization file, execute:
  19. //
  20. // % test i
  21. //
  22. // To synchronize a process, execute:
  23. //
  24. // % test n (where n is the process number)
  25. //
  26.  
  27. main(int argc, char **argv)
  28. {
  29.  
  30.     fprintf (stderr,"SEMAPHORE(1.0)\n");
  31.  
  32.     // must be 2 args
  33.     if (argc != 2)  {
  34.         printf ("Error: bad number of args.\n");
  35.         exit(0);
  36.     }
  37.  
  38.     // can't have a proc number greater than PROC_NUM
  39.     if (atoi(argv[1]) >= PROC_NUM) {
  40.         printf ("Invalid processor number: must be less than %d.\n",
  41.                         PROC_NUM);
  42.         exit(0);
  43.     }
  44.  
  45.     semaphore S(PROC_NUM);         // instatiate the semaphore object
  46.  
  47.     S.synchronize(argv[1]);    // perform the synchronization
  48.  
  49.     return(0);
  50. }
  51.